<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="公司名称">
              <a-input placeholder="请输入公司名称" v-model="queryParam.customName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="联系方式">
              <a-input placeholder="请输入联系方式" v-model="queryParam.contact"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('客户报备')">导出</a-button>
      <!-- <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down"/></a-button>
      </a-dropdown>
    </div>

    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action"></span>
        <span slot="action" slot-scope="text, record">
          <a-popconfirm v-if="record.approvalStatus === '2'" placement="topLeft" ok-text="确认" cancel-text="取消" @confirm="handleRevoke(record)">
            <a>撤销</a>
            <template slot="title">
              <p>是否撤销该条报价？</p>
            </template>
          </a-popconfirm>
          <a-divider type="vertical" v-if="record.approvalStatus === '2'"/>
          <a v-if="record.approvalStatus === '1' && record.reportStatus === '1'" @click="handleQuotation(record)"
            >报价</a
          >
          <a-divider v-if="record.approvalStatus === '1' && record.reportStatus === '1'" type="vertical" />
          <a
            v-if="['5', '6'].includes(record.approvalStatus) && [1, 4, 5, 6, 9].includes(userType)"
            @click="handleQuotation(record)"
            >修改报价</a
          >
          <a-divider
            v-if="['5', '6'].includes(record.approvalStatus) && [1, 4, 5, 6, 9].includes(userType)"
            type="vertical"
          />
          <a v-if="record.approvalStatus === '2' && userType === 4" @click="openModal(record, 1)">审批</a>
          <a-divider v-if="record.approvalStatus === '2' && userType === 4" type="vertical" />
          <a v-if="record.approvalStatus === '3' && userType === 5" @click="openModal(record, 2)">审批复核</a>
          <a-divider v-if="record.approvalStatus === '3' && userType === 5" type="vertical" />
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <gz-custom-info-modal ref="modalForm" @ok="modalFormOk" :cityList="cityList"></gz-custom-info-modal>
    <gz-report-modal ref="modalForm1" @ok="modalFormOk"></gz-report-modal>
    <QuotationModal
      ref="QuotationModalRef"
      :type="curType"
      :status="curStatus"
      :customId="customId"
      :packagePriceId="packagePriceId"
      :submitAfter="searchQuery"
    ></QuotationModal>
    <!-- <a-modal
      :visible="visible"
      :width="500"
      :title="modalTitle"
      :confirmLoading="submitLoad"
      destroyOnClose
      @cancel="handleCancel"
      @ok="handleApprove"
    >
      <a-form-model ref="ruleForm" :model="formData" layout="vertical">
        <a-form-model-item
          prop="isPass"
          label="审批状态"
          :rules="{
            required: true,
            message: '请选择审批状态'
          }"
        >
          <j-dict-select-tag
            type="list"
            v-model="formData.isPass"
            dictCode="bj_approve_status"
            placeholder="请选择审核意见"
          />
        </a-form-model-item>
        <a-form-model-item
          label="审核意见"
          prop="reviewComments"
          :rules="{
            required: formData.isPass === '2',
            message: '请输入审核意见',
            trigget: 'blur'
          }"
        >
          <a-input type="textarea" v-model="formData.reviewComments" placeholder="请输入审核意见" :maxLength="200" />
        </a-form-model-item>
      </a-form-model>
    </a-modal> -->
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import GzCustomInfoModal from './modules/GzCustomInfoModal'
import GzReportModal from './modules/GzReportModal'
import Area from '@/components/_util/Area'
import QuotationModal from '@/components/QuotationModal'
import { apiApprove, apiReApprove, apiGetOrganTree, apiRevoke } from '@/api/agentManage'

export default {
  name: 'GzCustomInfoList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    GzCustomInfoModal,
    GzReportModal,
    QuotationModal
  },
  data() {
    return {
      description: '客户信息管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '公司名称',
          align: 'center',
          dataIndex: 'customName'
        },
        {
          title: '机构代码',
          align: 'center',
          dataIndex: 'code'
        },
        {
          title: '公司地址',
          align: 'center',
          dataIndex: 'address'
        },
        {
          title: '联系方式',
          align: 'center',
          dataIndex: 'contact'
        },
        {
          title: '年用电量',
          align: 'center',
          dataIndex: 'dosage'
        },
        {
          title: '签约状态',
          align: 'center',
          dataIndex: 'type_dictText'
        },
        {
          title: '报备状态',
          align: 'center',
          dataIndex: 'reportStatus_dictText'
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'approvalStatus_dictText'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/gzCustomInfo/list',
        delete: '/gzCustomInfo/delete',
        deleteBatch: '/gzCustomInfo/deleteBatch',
        exportXlsUrl: '/gzCustomInfo/exportXls',
        importExcelUrl: 'gzCustomInfo/importExcel'
      },
      curType: '',
      curStatus: '',
      customId: '',
      packagePriceId: undefined,
      visible: false,
      modalTitle: '审核',
      submitLoad: false,
      formData: {
        isPass: '1',
        reviewComments: ''
      },
      curRow: {},
      curRowType: 1,
      cityList: []
    }
  },
  created() {
    this.getCity()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    userType() {
      return this.$store.state.user.info.userType
    }
  },
  methods: {
    // 获取地址
    async getCity() {
      const { code, result } = await apiGetOrganTree()
      if (code === 200) {
        this.cityList = result
      }
    },
    handleQuotation(record) {
      this.curType = record.userType
      this.customId = record.id
      this.curStatus = 'operate'
      this.packagePriceId = record.packagePriceId
      this.$refs.QuotationModalRef.visible = true
      // this.$refs.QuotationModalRef.quotationFormData.contractStartTime = record.staReportTime
      // this.$refs.QuotationModalRef.quotationFormData.contractEndTime = record.reportDays
    },
    // 撤销
    async handleRevoke(item) {
      const { code, data } = await apiRevoke({ id: item.id, approvalStatus: 6 })
      if (code === 200) {
        this.$message.success('操作成功！')
        this.searchQuery()
      }
    },
    openModal(row, type) {
      // if (type === 1) {
      //   this.modalTitle = '审批'
      // } else {
      //   this.modalTitle = '审批复核'
      // }
      // this.curStatus = 'approve'
      // this.curRow = row
      // this.curRowType = type
      // this.visible = true
      this.curType = row.userType
      this.customId = row.id
      if (type === 1) {
        this.curStatus = 'approve'
      } else {
        this.curStatus = 'reApprove'
      }
      this.packagePriceId = row.packagePriceId
      this.$refs.QuotationModalRef.visible = true
    },
    // 审批 type 1为审核，2为复核
    async handleApprove() {
      const apiFun = this.curRowType === 1 ? apiApprove : apiReApprove
      const params = { ...this.formData, id: this.curRow.id }
      await this.$refs.ruleForm.validate()
      this.submitLoad = true
      const { code, message } = await apiFun(params)
      this.submitLoad = false
      if (code === 200) {
        this.handleCancel()
        this.searchQuery()
        this.$message.success('操作成功！')
      } else {
        this.$message.success(message)
      }
    },
    handleCancel() {
      this.visible = false
      this.formData = {
        isPass: '1',
        reviewComments: ''
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
